<template>
    <div class="contract_main">
        <el-row class="head">
            <el-col :span="24">
                <h1 class="contract_title" style="margin-left: 30px!important;">产品分类维护</h1>
            </el-col>
        </el-row>
        <div class="content">
            <el-tabs>
                <div class="left">
                    <el-table :data="classilyList" style="margin-left: 20px" v-loading="loading">
                       <el-table-column label="分类编号" prop="proClassilyId" min-width="80" align="center">
                            <template slot-scope="s">
                                <div style="padding-left: 20px">{{s.row.proClassilyId}}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="proClassilyName" label="分类名" min-width="80" align="center">
                            <template slot-scope="s">
                                <div>{{s.row.proClassilyName}}</div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="faClassily" label="父级分类" min-width="80" align="center">
                            <template slot-scope="s">
                                <div>{{s.row.faProClassily?s.row.faProClassily.proClassilyName:"无"}}</div>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" min-width="100" align="center">
                            <template slot-scope="s">
                                <el-button type="primary" icon="el-icon-edit" circle @click="changeClassily(s.row)"></el-button>
<!--                                <el-button type="danger" icon="el-icon-delete" circle @click="deleteAll(s.$index, s.row)"></el-button>-->
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="right">
                    <span style="font-size: large">新增产品分类</span>
                    <el-form :model="classilyForm" :rules="classilyRule" ref="classilyRule" label-width="100px" style="margin-top:50px">
                        <el-form-item label="分类名称" prop="proClassilyName" >
                            <el-input v-model="classilyForm.proClassilyName" style="width:300px"></el-input>
                        </el-form-item>
                        <el-form-item label="父级分类" style="width:400px!important;">
                            <el-select v-model="classilyForm.faProClassily" value-key="proClassilyId" placeholder="请选择父类">
                                <el-option label="无" :value="null"></el-option>
                                <el-option v-for="i in classilyList" :key="i.proClassilyId" :label="i.proClassilyName" :value="i"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button style="float: right;margin-right: 30px;margin-top: 50px" @click="addClassily('classilyRule')" type="primary">确认添加</el-button>
                        </el-form-item>
                    </el-form>
                </div>
                <!--修改产品分类的弹框-->
                <el-dialog title="修改产品分类" :visible.sync="classilyVisible"  class="changeform" width="45%">
                    <el-form :model="classilyForm" :rules="classilyRule" ref="classilyRule" style="overflow: hidden">
                        <el-form-item label="分类名称" :label-width="formLabelWidth" prop="proClassilyName" >
                            <el-input v-model="classilyForm.proClassilyName" autocomplete="off"></el-input>
                        </el-form-item>
                        <el-form-item label="父级分类" :label-width="formLabelWidth" style="width:420px!important;">
                            <el-select v-model="classilyForm.faProClassily" value-key="proClassilyId" placeholder="请选择父类">
                                <el-option label="无" :value="null"></el-option>
                                <el-option v-for="i in classilyList" :key="i.proClassilyId" :label="i.proClassilyName" :value="i"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item style="margin-left:180px;">
                            <el-button type="primary" @click="addClassily('classilyRule')">保 存</el-button>
                            <el-button @click="cancel()">取 消</el-button>
                        </el-form-item>
                    </el-form>
                </el-dialog>
            </el-tabs>
        </div>
    </div>
</template>

<script>
    export default {
        name: "classily",
        data: function() {
            return {
                loading:true,
                //修改产品分类的弹框的状态
                classilyVisible:false,
                //产品分类列表的数据
                classilyList: [],
                //新增、修改产品分类的数据
                classilyForm: {
                    proClassilyId:0,
                    proClassilyName: ''
                },
                //产品分类的规则
                classilyRule: {
                    proClassilyName: [
                        { required: true, message: '请输入分类名称', trigger: 'blur' }
                    ]
                },
                //表单宽度
                formLabelWidth: '120px',
            };
        },
        methods: {
            //临时的删除方法
            deleteAll(){
                this.$confirm('此操作将删除选中产品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //产品分类列表查询
            classily(){
                this.loading = true
                this.axios.get("api/product/classily-list2").then(v=>{
                    this.classilyList=v.data;
                    this.loading = false
                }).catch(() => {
                    this.loading = false
                    this.global.mes_error("获取数据失败！")
                })
            },
            //新增产品分类
            addClassily(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.axios.post("api/product/add-classily", this.classilyForm)
                            .then(v => {
                                window.console.log(v)
                                this.classily();
                                this.clearClassilyForm();
                                this.global.mes_success("操作成功");
                                this.classilyVisible = false;
                            }
                        ).catch(err => {
                            window.console.log(err)
                        })
                    }else {
                        this.global.mes_warning("分类名称不能为空！")
                        return false;
                    }
                });
            },
            //清除新增、修改产品分类的数据
            clearClassilyForm(){
                this.classilyForm={}
            },
            //修改产品分类
            changeClassily(row) {
                this.classilyVisible = true;
                this.classilyForm.proClassilyId=row.proClassilyId;
                this.classilyForm.proClassilyName=row.proClassilyName;
                this.classilyForm.faProClassily = row.faProClassily;
                
            },
            //取消修改
            cancel(){
                this.clearClassilyForm();
                this.classilyVisible = false;
            }
        },
        watch: {

        },
        created() {
            this.classily();
        }
    }
</script>

<style scoped>
    *{margin: 0;padding: 0;font-size: 14px}
    .contract_main{background: #e4e7ee}
    .head{height:70px;background: #f8f6f8;line-height: 70px}
    .contract_title{display: inline-block;font-weight: unset;margin-left: 20px;padding-right: 3px;font-size: 25px}
    .el-col span{color: #b4b4bc}
    .content{margin: 20px;background: #fcfbfd;width: 100%-40px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);padding-bottom: 20px}
    .el-col span {color:grey;}
    .right{width:45%;float:left;margin-left: 60px;padding-top:20px }
    .left{width:45%;float:left}
    .left el-button{width: 20px;height:50px!important;}
    .changeform .el-input{width: 300px}
</style>